﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Argument checking</title>
  <keywords>
    <keyword translate="true">Argument</keyword>
    <keyword translate="true">Exceptions</keyword>
    <keyword translate="true">Validation</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Argument checking</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">It is best practice to always check if the input to a method is correct. If not, an exception should be thrown. Most people do not check for exceptions correctly and lots of null reference exceptions inside a deep stacktrace are hard to solve.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel does check the input on every method. Normally, a check would look like this:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CheckForException(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> obj)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> (obj == </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">null</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    {</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">        </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">throw</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> ArgumentNullException(&quot;obj&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">However, Catel extensively logs all behavior, thus all the checks started to look like </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">this</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">:</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CheckForException(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> obj)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> (obj == </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">null</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    {</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">        Log.Debug(&quot;Argument &apos;obj&apos; is null in CheckForException&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">        </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">throw</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> ArgumentNullException(&quot;obj&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Handling input correctly in such a case takes a lot of space and repetitive code. Therefore the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Argument" styleclass="Normal" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Argument</link><text styleclass="Normal" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> class is developed. This way, it is very simple to check for arguments:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CheckForException(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> obj)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    Argument.IsNotNull(&quot;obj&quot;, obj);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Or, if a range should be checked:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CheckForException(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">int</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> myInt)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    Argument.IsNotOutOfRange(&quot;myInt&quot;, myInt, 0, 10);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A final example is to check whether a type implements a specific interface:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CheckForException(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> obj)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    Argument.ImplementsInterface(&quot;obj&quot;, obj, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">(INotifyPropertyChanged));</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
  </body>
</topic>
